package com.mvc.controller;

import com.mvc.dao.DepartmentDao;
import com.mvc.dao.UserDao;
import com.mvc.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Created by zhudong on 2016/9/4.
 */
@RequestMapping("/user")
@Controller
public class UserController {

    @Autowired
    private UserDao userDao;

    @Autowired
    private DepartmentDao departmentDao;

    @RequestMapping("/list")
    public String list(Map<String,Object> map){
        Collection<User> users = userDao.getAll();
        map.put("users",users);
        System.out.println(users);
        return "list";
    }

    @ResponseBody
    @RequestMapping("/listjson")
    public Collection<User> listjson(){
        Collection<User> users = userDao.getAll();
//        map.put("users",users);
//        System.out.println(users);
        return users;
    }

    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public String input(Map<String,Object> map){
        map.put("departments", departmentDao.getAll());
        map.put("user",new User());
        return "input";
    }

//    @RequestMapping(value="/user/{id}",method = Re)

    @RequestMapping(value = "/user",method = RequestMethod.POST)
    public String create(User user){
        userDao.save(user);
        return "redirect:/user/list";
    }

    @RequestMapping(value = "/user",method = RequestMethod.DELETE)
    public String delete(User user){
        System.out.println(user);
        return "redirect:/user/list";
    }

    @RequestMapping(value = "/user",method = RequestMethod.PUT)
    public String update(User user){
        System.out.println(user);
        return "redirect:/user/list";
    }
}
